import { Form } from 'antd';

import { useActiveKeys } from './useActiveKeys';
import { useCollapseChange } from './useCollapseChange';

/**
 * @useCollapseForm 折叠表单hooks
 * @param options 配置项
 * @returns
 */
export function useCollapseForm(options: CollapseFormAPI.CollapseFormProps) {
  // 创建 Form 实例，用于管理所有数据状态
  const [form] = Form.useForm();

  const { schemas, name, isReadOnly = false, basicForm = form } = options;

  /**
   * @activeKeys 折叠面板的所有 key
   */
  const { activeKeys } = useActiveKeys({ schemas });
  /**
   * @activeKey 当前激活折叠面板的 key
   * @changeActiveKey 切换面板的回调
   */
  const { activeKey, changeActiveKey } = useCollapseChange({ activeKeys });

  return [
    {
      form: basicForm,
      name,
      schemas,
      isReadOnly,
      activeKey,
      activeKeys,
      changeActiveKey,
    } as any,
    form,
  ];
}
